package com.tianpengtech.admin;

import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.ext.interceptor.POST;
import com.tianpengtech.common.GlobalConfigConstant;
import com.tianpengtech.common.model.account.Account;
import com.tianpengtech.common.plugin.shiro.ext.ClearShiro;
import com.tianpengtech.common.util.JsonUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;

//只能是admin角色进行访问
@RequiresRoles(value = { "admin"})
public class AdminCommonController extends Controller {


    public void index(){

        renderTemplate("index_new.html");
    }


    @ClearShiro
    public void login(){

        renderTemplate("login.html");
    }


    @Before(POST.class)
    @ClearShiro
    public void doLogin(){

        String userName=getPara(Account.USERNAME, GlobalConfigConstant.DEFAULT_VALUE_EMPTY);
        String userPass=getPara(Account.USERPASS, GlobalConfigConstant.DEFAULT_VALUE_EMPTY);
        UsernamePasswordToken token = new UsernamePasswordToken(userName,userPass);
        token.setRememberMe(false);
        Subject subject = SecurityUtils.getSubject();
        subject.login(token);
        renderJson(JsonUtil.printSuccess("登录成功..."));
    }

    @RequiresAuthentication
    public void logout() {
        Subject currentUser = SecurityUtils.getSubject();
        currentUser.logout();
        this.redirect("/admin/login");
    }

    public void welcome(){

        renderTemplate("welcome.html");
    }
}
